package thread;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/28
 * Time:16:09
 * 内存不可见性
 **/
public class ThreadDemo20 {

    private static volatile boolean flag = false;

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!flag) {

                }
                System.out.println("终止执行");
            }
        });
        t1.start();
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("设置flag为true");
                flag = true;
            }
        });
        t2.start();
    }
}
